<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/27
 * Time: 16:33
 */

namespace app\admin\controller;
use app\BaseController;
use app\services\UtilService as Util;
use app\services\JsonService as Json;
use think\facade\Route as Url;

class CourseSet extends BaseController
{

    public function choose_course(){
        $param = $this->request->param();
        if($this->request->isAjax()){
            if(!$param['course_type']){
                return $this->failed('数据不存在!');
            }else{
                $redirect_url= '/admin/CourseSet/create.html?course_type=1';
                return $this->successful('成功!',$redirect_url);
            }
        }
    }

    public function create(){
        $param = $this->request->param();
        $this->assign('course_type',$param['course_type']);
        return $this->fetch();
    }


    public function save(){
        $param = $this->request->param();
        if($this->request->isAjax()){
            if(!$param['course_type']){
                return $this->failed('添加失败!');
            }else{
                return $this->successful('添加成功!');
            }
        }
        $data = Util::postMore([
            'title',
            'sub_title',
            'tag_ids',
            ['cat_ids',[]],
            ['serial',''],
            ['cover_image',''],
            'desc',
            ['status',1],
            ]);
        if(!$data['title']) return Json::fail('请输入分类名称');
        if(count($data['image']) != 1) return Json::fail('请选择分类图片，并且只能上传一张');
        if($data['sort'] < 0) return Json::fail('排序不能是负数');
        $data['add_time'] = time();
        $data['image'] = $data['image'][0];
        $new_id = $data['new_id'];
        unset($data['new_id']);
        $res = ArticleCategoryModel::create($data);
        if(!ArticleModel::saveBatchCid($res['id'],implode(',',$new_id))) return Json::fail('文章列表添加失败');
        return Json::successful('添加分类成功!');



    }
}